package QRCG;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SearchGamesServlet
 */
@WebServlet("/SearchGamesServlet")
public class SearchGamesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);

		String searchParam = request.getParameter("searchParam");
		System.out.println(searchParam);
		String searchCateg = request.getParameter("searchCateg");
		System.out.println(searchCateg);

		boolean success = true;

		if (success) {
			DatabaseManager conn = new DatabaseManager();
			try {
				conn.connect();
				ArrayList<HashMap<String, Object>> games = new ArrayList<HashMap<String, Object>>();

				games=conn.getSearchedGames(searchParam, searchCateg);

				session.setAttribute("sgames", games);
				session.setAttribute("sparam", searchParam);
				session.setAttribute("scat", searchCateg);
				session.setAttribute("searchedB", "searched");
				System.out.println("servlet "+session.getAttribute("searchedB"));
				response.sendRedirect("ViewGames.jsp");

				}
			 catch (SQLException e) {
				 success = false;
			} finally {
			  conn.close();
			}
		}


	}

}
